package com.lobaby.tools.modules.pdf.controller;

import com.lobaby.tools.base.controller.BaseController;
import com.lobaby.tools.common.utils.UploadUtil;
import com.lobaby.tools.model.ConsoleResult;
import com.lobaby.tools.modules.pdf.service.IPdfService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * @author Administrator
 *         Created by Administrator on 2021/3/1 0001.
 */
@Controller
@RequestMapping("/pdf")
public class PdfController extends BaseController {
    private static final Logger LOGGER = LoggerFactory.getLogger(PdfController.class);

    @Autowired
    private IPdfService pdfService;

    @RequestMapping(value = "/pdfsplit", method = RequestMethod.POST)
    @ResponseBody
    public ConsoleResult pdfsplit(HttpServletRequest request) {
        try {
            List<MultipartFile> files = UploadUtil.getFiles(request);
            HashMap<String, String> requestParam = getRequestParam(request);
            return pdfService.pdfsplit(files, requestParam);
        } catch (Exception e) {
            LOGGER.error("", e);
            return ConsoleResult.getErrObj("转换异常！");
        }
    }

    @RequestMapping(value = "/pdfmerge", method = RequestMethod.POST)
    @ResponseBody
    public ConsoleResult pdfmerge(HttpServletRequest request) {
        try {
            List<MultipartFile> files = UploadUtil.getFiles(request);
            return pdfService.pdfmerge(files);
        } catch (Exception e) {
            LOGGER.error("", e);
            return ConsoleResult.getErrObj("转换异常！");
        }
    }

    @RequestMapping(value = "/pictopdf", method = RequestMethod.POST)
    @ResponseBody
    public ConsoleResult picToPdf(HttpServletRequest request) {
        try {
            List<MultipartFile> files = UploadUtil.getFiles(request);
            return pdfService.picToPdf(files);
        } catch (Exception e) {
            LOGGER.error("", e);
            return ConsoleResult.getErrObj("转换异常！");
        }
    }

    @RequestMapping(value = "/pdftopng", method = RequestMethod.POST)
    @ResponseBody
    public ConsoleResult pdftopng(MultipartFile file) {
        try {
            return pdfService.pdftopng(file);
        } catch (Exception e) {
            LOGGER.error("", e);
            return ConsoleResult.getErrObj("转换异常！");
        }
    }

    @RequestMapping(value = "/wordtopdf", method = RequestMethod.POST)
    @ResponseBody
    public ConsoleResult wordtopdf(MultipartFile file) {
        try {
            return pdfService.wordtopdf(file);
        } catch (Exception e) {
            LOGGER.error("", e);
            return ConsoleResult.getErrObj("转换异常！");
        }
    }
}
